﻿@using Seal.Model
@using Seal.Helpers
@{
    Report report = Model;
    ReportView view = report.CurrentView;
    ReportView modelView = view.ModelView;
    ReportModel reportModel = modelView.Model;
    ResultPage page = report.CurrentPage;
    if (page == null) { return; }

    string gaugeId = "g" + Helper.NewGUID(), previewTextId = "pv" + gaugeId, gaugeContainerId = "co" + gaugeId;
    page.SetIdentifier(ResultPageIdentifierType.GaugeCanvas, gaugeContainerId);

    ResultTable table = page.DataTable;
    string value = null, min = null, max = null, animation = null;
    if (table.ColumnCount > 0 && table.RowCount > 1)
    {
        value = table[table.BodyStartRow, 0].Value.ToString();
        if (table.ColumnCount > 1) {
            min = table[table.BodyStartRow, 1].Value.ToString();            
        }
        if (table.ColumnCount > 2) {
            max = table[table.BodyStartRow, 2].Value.ToString();     
        }
        if (table.ColumnCount > 3) {
            animation = table[table.BodyStartRow, 3].Value.ToString();        
        }
    }

    min = min ?? view.GetValue("gauge_min");
    max = max ?? view.GetValue("gauge_max");
    animation= animation ?? view.GetValue("gauge_animation");

    string dynamicLabels = "";
    int mini, maxi;
    int step = view.GetNumericValue("gauge_step"), division = 0;
    if (step > 0 && int.TryParse(min, out mini) && int.TryParse(max, out maxi))
    {
        for (var i = mini; i<=maxi; i += step)
        {
            dynamicLabels += (i != mini ? ",": "") + $"{i}";
        }

        division = (maxi - mini) / step;
    }
    var labels = view.GetValue("gauge_staticlabels");
    if (!string.IsNullOrEmpty(labels))
    {
        labels = labels.Replace("{LABELS}", dynamicLabels);
    }
    var ticks = view.GetValue("gauge_ticks");
    if (!string.IsNullOrEmpty(ticks))
    {
        ticks = ticks.Replace("{DIVISION}", division.ToString());
    }

    if (value == null)
    {
        throw new Exception("No value in the result table of the model.");
    }
}

<div id="@gaugeContainerId" class="@Raw(view.GetValue("gauge_class"))" style="@Raw(view.GetValue("gauge_css"))">
      <div id="@previewTextId" class="@Raw(view.GetValue("preview_text_class"))" style="@Raw(view.GetValue("preview_text_css"))"></div>
      <canvas id="@gaugeId" @Raw(Helper.AddIfNotEmpty("width='", view.GetValue("gauge_width"), "'")) @Raw(Helper.AddIfNotEmpty("height='", view.GetValue("gauge_height"), "'"))></canvas>
</div>

<script>
$(document).ready(function () {
    //check https://github.com/bernii/gauge.js
    var opts = {
        @Raw(view.GetValue("gauge_options"))
        @Raw(Helper.AddIfNotEmpty("pointer:{", view.GetValue("gauge_pointer"), "},\r\n")) 
        @Raw(Helper.AddIfNotEmpty("renderTicks:{", ticks, "},\r\n")) 
        @Raw(Helper.AddIfNotEmpty("percentColors:[", view.GetValue("gauge_percentcolors"), "],\r\n")) 
        @Raw(Helper.AddIfNotEmpty("staticLabels:{", labels, "},\r\n")) 
        @Raw(Helper.AddIfNotEmpty("staticZones:[", view.GetValue("gauge_staticzones"), "],\r\n")) 
    };

    var @(gaugeId) = new Gauge(document.getElementById("@gaugeId"), @view.GetValue("gauge_height"), @view.GetValue("gauge_width")).setOptions(opts); 
    @(gaugeId).setTextField(document.getElementById("@previewTextId"));
    @Raw(Helper.AddIfNotEmpty(gaugeId + ".setMinValue(", min, ");"))
    @Raw(Helper.AddIfNotEmpty(gaugeId + ".maxValue=", max, ";"))
    @(gaugeId).animationSpeed = @animation; 
    @(gaugeId).set(@value); 
});
</script>